package edu.uis.csc478.spring09.threeoxen.requestprocessing.impl;

import edu.uis.csc478.spring09.threeoxen.persistence.PersistenceFactoryLoader;
import edu.uis.csc478.spring09.threeoxen.persistence.PersistenceManager;
import edu.uis.csc478.spring09.threeoxen.persistence.user.User;
import edu.uis.csc478.spring09.threeoxen.persistence.user.UserRepository;
import edu.uis.csc478.spring09.threeoxen.requestprocessing.RequestProcessingException;
import edu.uis.csc478.spring09.threeoxen.requestprocessing.UserManagementRequestProcessor;

/**
 * User: Chris Logan
 * Date: Apr 14, 2009
 * Time: 12:08:23 PM
 */
public class UserManagementRequestProcessorImpl implements UserManagementRequestProcessor {

    @Override
    public boolean login(String userId, String password) throws RequestProcessingException {
        UserRepository repository = null;
        try {
            repository = (UserRepository) PersistenceManager.getInstance().getRepository(PersistenceManager.USER_REPOSITORY);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RequestProcessingException("Error loading repository", e);
        }
        User user = repository.getUser(userId);
        if (user == null) {
            return false;
        }
        if (user.getPassword() == password) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void createNewUser(String userId, String password, String email, String userType) throws RequestProcessingException {
        User user = PersistenceFactoryLoader.getInstance().getUserObjectFactory().createUser();
        user.setUserName(userId);
        user.setPassword(password);
        user.setEmailAddress(email);
        user.setType(userType);
        UserRepository repository = null;
        try {
            repository = (UserRepository) PersistenceManager.getInstance().getRepository(PersistenceManager.USER_REPOSITORY);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RequestProcessingException("Error loading repository", e);
        }
        if (repository.userExists(userId)) {
            throw new RequestProcessingException("User " + userId + " already exists");
        }
        try {
            repository.addUser(user);
            PersistenceManager.getInstance().updateRepository(repository);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RequestProcessingException("Unable to create user " + userId, e);
        }
    }

    @Override
    public void modifyUser(String userId, String password, String userType) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public User getUser(String userId) throws RequestProcessingException {
        try {
            return ((UserRepository) PersistenceManager.getInstance().getRepository(PersistenceManager.USER_REPOSITORY)).getUser(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RequestProcessingException("Error loading repository", e);
        }
    }
}
